// pages/product-details/product-details.js
import { URL, SER_KEY } from "../../config.js"
import { POST, GET } from "../../utils/request.js"
/// 获取倍率
const raterpx = 750.0 / wx.getSystemInfoSync().windowWidth;
/// 获取canvas转化后的rpx
const rate = function (rpx) {
  return rpx / raterpx
};
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showBox: false,
    indicatorDots: false, //  是否显示面板指示点
    autoplay: false, // 是否自动切换
    circular: true, // 是否采用衔接滑动
    current: 0, // 当前所在页面的 index
    interval: 500, // 自动切换时间间隔
    duration: 500,// 滑动动画时长
    //swiper滑动的数组
    swiperCurrent: 0,
    showBox:false,//选择弹框的显示与隐藏
    animationData:{},
    animationData2:{},
    head_img:'',//头像
    userName:'',//用户名
    show:false,
    sharePoster: true,//海报显示隐藏
    productId:'',//商品id
    productMsg:{},
    storeMsg:{},
    choosed: '',//已选择
    choosePrice: null,//选择规格后的价格
    choosedSpecId: '',//选中的商品规格id
    choosedLspecification: '',//选中的商品规格
    choosedLattribute: '',//选中的商品属性
    styleIndex: null,
    typeIndex: null,
    select_num: 1,
    shopId:null,
    shopName:null,
    shopImage:null,
    nickName:'',//昵称
    avataUrl:'',//头像
    codeUrl:'',
    contBox:false,
    choosePic:'',
    oriPrice:null,//选择的初始价格
    detailPic:[],
    sysWidth:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let shId = wx.getStorageSync('id')
    this.setData({ shopId: shId,sysWidth:wx.getSystemInfoSync().windowWidth, })
    console.log('测试', shId)
    console.log('id', options)
    if (!options.shopName){
      var storeMsg = JSON.parse(wx.getStorageSync("shopData"))
      this.setData({
        storeMsg: storeMsg,
        shopName: storeMsg, 
        shopImage: storeMsg.shopImage
      })
    }else{
      this.setData({
        shopName: options.shopName,
        shopImage: options.shopImage
      })
    }
    if (options.scene) {
      const scene = decodeURIComponent(options.scene)
      var appId = this.getUrlParam('appId', scene)
      var productId = this.getUrlParam('productId', scene)
      var shopId = this.getUrlParam('shopId', scene)
      // console.log(scene)
      this.setData({
        productId: productId,
        shopId: shopId,
        appId:appId
      })
    }
    console.log('分享参数', options, options.shopId)
    if ( options.shopId) {
      console.log('分享参数', options, options.shopId)
      this.setData({
        productId: options.id,
        shopId: options.shopId,
        shopName: options.shopName,
        shopImage: options.shopImage
      })
    }
    if (options.id){
      this.setData({
        productId: options.id,
      })
    }
    
  },
  // 选择属性
  chooseStyle: function (e) {
    let that = this;
    let name = that.data.choosedLspecification
    that.setData({
      styleIndex: e.currentTarget.dataset.index,
      choosedLattribute: e.currentTarget.dataset.name,
      // choosed: name ? name + '/' + e.currentTarget.dataset.name : e.currentTarget.dataset.name
    })
  },
  onGotUserInfo(e){
    console.log(e)
    let avataUrl = e.detail.userInfo.avatarUrl
    let that=this
    this.setData({
      avataUrl: e.detail.userInfo.avatarUrl,
      nickName: e.detail.userInfo.nickName
    }, () => that.getAvaterInfo(avataUrl) )
  },
  draw(){
    let that = this;
    wx.showLoading({
      title: '生成中...',
      mask: true,
    })
    let avataUrl = that.data.avataUrl;
    let productImg = that.data.productImg
    let title = that.data.productMsg.name;
    let nickName = that.data.nickName
    let dec = that.data.productMsg.product_desc;
    let topPrice = that.data.productMsg.price;
    let codeUrl = that.data.codeUrl
    const ctx = wx.createCanvasContext('myCanvas'); //创建画布
    // canvasContext.font = value //示例
    // ctx.font = `normal 10px sans-serif`//设置字体大小，默认10
    var width = "";
    var color = '';
    wx.createSelectorQuery().select('#canvas-container').boundingClientRect(function (rect) {
      var height = rect.height;
      var right = rect.right;
      width = rect.width * 0.8;
      var left = rect.left-rate(15);
      var top=rect.top-rate(70)
      ctx.setFillStyle('#fff');
      ctx.fillRect(0, 0, rect.width, height);
      // console.log(width,rect,left)
      /// 直径
      const diameter = rate(54);
      /// 圆参数 
      const arc = {
        radii: diameter / 2,
        x: rate(40),
        y: rate(800)
      };
      
      console.log(nickName)
      if (nickName){
        ctx.setFontSize(14);
        ctx.setFillStyle('#000');
        ctx.setTextAlign('left');
        ctx.setTextBaseline("top");
        ctx.fillText(nickName, left+50,top-26,width,45)//绘制文本
        ctx.setFontSize(12);
        ctx.setFillStyle('#999999');
        ctx.fillText('给你推荐了一个好东西', left+50,top-4,width,45)//绘制文本

      }
      console.log(avataUrl)
      if(avataUrl){
        ctx.save();
        ctx.beginPath()
        ctx.arc(left + 20, top - 8, 20, 0, Math.PI * 2, false);
        ctx.setStrokeStyle('#ffffff') //设置边的
        ctx.clip();
        ctx.drawImage(avataUrl, left, top - 28, 40, 40)
        ctx.closePath()
        ctx.stroke()
        ctx.restore()
       
        // ctx.drawImage(avataUrl, left, top-28,40, 40 );
        // that.LeftArc(ctx, 40, 40, 20, 0.5)
      }  
      
      //商品图片
      console.log(productImg)
      if (productImg) {

        // ctx.drawImage(productImg, left, top+24, width*1, height*0.55);
        // ctx.setFontSize(14);
        // ctx.setFillStyle('#fff');
        // ctx.setTextAlign('left');
  
        // 图片的x坐标
        let bg_x = left
        // 图片的y坐标
        let bg_y = top + 24
        // 图片宽度
        let bg_w = width * 1
        // 图片高度
        let bg_h = height * 0.55
        // 图片圆角
        let bg_r = 4

        // 绘制海报背景图片圆角
        // ctx.save()
        // ctx.beginPath()
        // ctx.arc(bg_x + bg_r, bg_y + bg_r, bg_r, Math.PI, Math.PI * 1.5)
        // ctx.arc(bg_x + bg_w - bg_r, bg_y + bg_r, bg_r, Math.PI * 1.5, Math.PI * 2)
        // ctx.arc(bg_x + bg_w - bg_r, bg_y + bg_h - bg_r, bg_r, 0, Math.PI * 0.5)
        // ctx.arc(bg_x + bg_r, bg_y + bg_h - bg_r, bg_r, Math.PI * 0.5, Math.PI)
        // ctx.clip()
        ctx.drawImage(productImg, bg_x, bg_y, bg_w, bg_h)
        // 恢复之前保存的绘图上下文
        // ctx.restore()
      }
      
      //标题
      // console.log(title)
      if (title) {
        // ctx.setFontSize(14);
        // ctx.setFillStyle('#000');
        // ctx.setTextAlign('left');
        // ctx.fillText(title, left,top+38+height*0.55, width + 25);
        that.drawText({
          ctx: ctx,
          str: title,
          maxLine: 1,
          maxWidth: rate(346),
          style: '#000',
          x: left,
          y: top + 38 + height * 0.55,
          height: rate(30),
          fontSize: rate(28)
        })
      }
      //描述
      // console.log(title)
      if (dec) {
        // ctx.setFontSize(12);
        // ctx.setFillStyle('#999999');
        // ctx.setTextAlign('left');
        // ctx.fillText(dec, left, top+68+height*0.55,width + 45);
        that.drawText({
          ctx: ctx,
          str: dec,
          maxLine: 1,
          maxWidth: rate(320),
          x: left,
          y: top + 60 + height * 0.55,
          height: rate(26),
          fontSize: rate(24)
        })
      }
      //价格
      if (topPrice) {
        ctx.setFontSize(18);
        ctx.setFillStyle('#FB8558');
        ctx.setTextAlign('left');
        ctx.fillText('￥' + topPrice,left,top+78+height*0.55,  width + 70);
      }
      //  绘制二维码
      console.log(codeUrl)
      if (codeUrl) {
        ctx.save();
        ctx.drawImage(codeUrl, left + rate(335), top+38+height*0.55, width/3, width/3)
        ctx.setFontSize(10);
        ctx.setFillStyle('#000');
        ctx.restore()

        // ctx.fillText("微信扫码或长按识别", left + 150, top+35+height*0.55+(width/3),width/3,width/3);
      }

    }).exec()
    setTimeout(function () {
      ctx.draw();
      wx.hideLoading();
    }, 1000)
  },
 
  getUrlParam(key, url) {
    // 获取参数
    var url = url;
    // 正则筛选地址栏
    var reg = new RegExp('(^|&)' + key + '=([^&]*)(&|$)');
    // 匹配目标参数
    var result = url.substr(1).match(reg);
    //返回参数值
    return result ? decodeURIComponent(result[2]) : null;
  },
  /// 绘制文本
  drawText: function (options) {
    /// 获取总行数
    var allRow = Math.ceil(options.ctx.measureText(options.str).width / options.maxWidth);
    /// 限制行数
    var count = allRow >= options.maxLine ? options.maxLine : allRow,
      /// 当前字符串的截断点
      endPos = 0;
    /// 设置文字颜色
    options.ctx.setFillStyle(options.style ? options.style : '#353535');
    /// 设置字体大小
    options.ctx.setFontSize(options.fontSize ? options.fontSize : rate(20));
    /// 循环截断
    for (var j = 0; j < count; j++) {
      /// 当前剩余的字符串
      var nowStr = options.str.slice(endPos),
        /// 每一行当前宽度
        rowWid = 0,
        /// 每一行顶部距离
        y = options.y + (count == 1 ? 0 : j * options.height);
      /// 如果当前的字符串宽度大于最大宽度，然后开始截取
      if (options.ctx.measureText(nowStr).width > options.maxWidth) {
        for (var m = 0; m < nowStr.length; m++) {
          /// 计算当前字符串总宽度
          rowWid += options.ctx.measureText(nowStr[m]).width;
          if (rowWid > options.maxWidth) {
            /// 如果是最后一行
            if (j === options.maxLine - 1) {
              options.ctx.fillText(nowStr.slice(0, m - 1) + '...', options.x, y);
            } else {
              options.ctx.fillText(nowStr.slice(0, m), options.x, y);
            }
            /// 保留下次截断点
            endPos += m;
            console.log(endPos)
            break;
          }
        }
      } else { /// 如果当前的字符串宽度小于最大宽度就直接输出
        options.ctx.fillText(nowStr.slice(0), options.x, y);
      }
    }
  },
  /**
   * 先下载头像
   */
  getAvaterInfo: function (img) {
    wx.showLoading({
      title: '加载中...',
      mask: true,
    });
    var that = this;
    console.log(img)
    wx.downloadFile({
      url: img, //头像图片路径
      success: function (res) {
        wx.hideLoading();
        if (res.statusCode === 200) {
          var avataUrl = res.tempFilePath; //下载成功返回结果
          console.log('下载头像',avataUrl)
          that.setData({
            avataUrl: avataUrl
          })
          // console.log(avaterSrc)
        } else {
          wx.showToast({
            title: '头像下载失败！',
            icon: 'none',
            duration: 2000,
            success: function () {
             
            }
          })
        }
      }
    })
  },
  /**
   * 下载商品图片
   */
  getProductImg: function (img) {
    wx.showLoading({
      title: '加载中...',
      mask: true,
    });
    var that = this;
    console.log(img)
    // let newImg = img.slice(0, 4) + 's' + img.slice(4)
    // console.log(newImg)
    wx.downloadFile({
      url: img, //图片路径
      success: function (res) {
        console.log(res)
        wx.hideLoading();
        if (res.statusCode === 200) {
          var pic = res.tempFilePath; //下载成功返回结果
          console.log('下载商品图片',pic)
          that.setData({
            productImg: pic
          })
        } else {
          wx.showToast({
            title: '头像下载失败！',
            icon: 'none',
            duration: 2000,
            success: function () {

            }
          })
        }
      }
    })
  },
  //获取二维码
  getwxacode: function () {
    let that = this;
    let url = URL.HTTP_BASE_INTRANET + SER_KEY.SER_KEY27;
    var sessionId = wx.getStorageSync('session_id');
    let data = {
      appId: getApp().globalData.appId,
      sessionId: sessionId,
      productId: that.data.productMsg.id,
      shopId:wx.getStorageSync('id')
    };
    GET(url, data, (ret) => {
      console.log(ret)
      if (ret.data.res == "SUCCESS") {
        // console.log('下载二维码',ret.data.url)   
        this.setData({
          codeUrl: ret.data.url
        }, () => that.getcodeUrlInfo(ret.data.url))
        // that.deleteCodePic(that.data.codeUrl)
      }else{
        wx.showToast({
          title: ret.data.errMsg,
          icon: 'none'
        })
        setTimeout(function () {
          if (ret.data.errMsg == '登录过期') {
            wx.navigateTo({
              url: '/pages/authorized-login/authorized-login?type=0&id=' + that.data.productId,
            })
          }
        }, 1000)
      }
    })

  },
  /**
   * 下载商品图片
   */
  getcodeUrlInfo: function (img) {
    wx.showLoading({
      title: '加载中...',
      mask: true,
    });
    var that = this;
    console.log(img)
    // let newImg = img.slice(0, 4) + 's' + img.slice(4)
    // console.log(newImg)
    wx.downloadFile({
      url: img, //头像图片路径
      success: function (res) {
        console.log(res)
        wx.hideLoading();
        if (res.statusCode === 200) {
          var pic = res.tempFilePath; //下载成功返回结果
          console.log('下载二维码',pic)
          that.setData({
            codeUrl: pic
          }, () => that.draw())
          // console.log(avaterSrc)
        } else {
          wx.showToast({
            title: '头像下载失败！',
            icon: 'none',
            duration: 2000,
            success: function () {

            }
          })
        }
      }
    })
  },
  //点击保存到相册
  saveShareImg: function () {
    var that = this;
    wx.showLoading({
      title: '正在保存',
      mask: true,
    })
    setTimeout(function () {
      wx.canvasToTempFilePath({
        canvasId: 'myCanvas',
        success: function (res) {
          wx.hideLoading();
          console.log(res)
          var tempFilePath = res.tempFilePath;
          wx.saveImageToPhotosAlbum({
            filePath: tempFilePath,
            success(res) {
              wx.showModal({
                content: '图片已保存到相册，赶紧晒一下吧~',
                showCancel: false,
                confirmText: '好的',
                confirmColor: '#333',
                success: function (res) {
                  console.log(res)

                  if (res.confirm) {
                    // that.deleteCodePic(that.data.codeUrl);
                  }
                },

                fail: function (res) { }
              })
            },
            fail: function (res) {
              wx.showToast({
                title: res.errMsg,
                icon: 'none',
                duration: 2000
              })
            }
          })
        }
      });
    }, 1000);
  },
  //联系客服
  contact(){
    this.data.contactCode?this.setData({
      contBox:true,
      show:true
    }):''
  },
  
  contClose() {
    this.setData({
      contBox: false,
      show: false
    })
  },
  //立即购买
  buy() {
    var that = this
    var id = that.data.productId
    var sessionId = wx.getStorageSync('session_id');
    console.log(sessionId)
    if (sessionId!='') {
      let choosedAttrName = '';
      let num = that.data.select_num;
      if (that.data.productMsg.lattribute && that.data.productMsg.lattribute.length) {
        choosedAttrName = that.data.choosedLspecification + '-' + that.data.choosedLattribute;
      } else {
        choosedAttrName = that.data.choosedLspecification;
      }
      let url = URL.HTTP_BASE_INTRANET + SER_KEY.SER_KEY18;
      let data = {
        appId: getApp().globalData.appId,
        sessionId: sessionId,
        shopId: that.data.shopId,
        productId: that.data.productMsg.id,
        productCount: num,
        attrName: choosedAttrName||'默认',
        specId: that.data.choosedSpecId||that.data.productMsg.lspecification[0].id
      };
      GET(url, data, (resData) => {
        console.log(resData);
        if (resData.data.res == "SUCCESS") {
          wx.navigateTo({
            url: '../order-pay/order-pay',
          })
        }else{
          wx.showToast({
            title: resData.data.errMsg,
            icon:'none'
          })
          setTimeout(function () {
            if (resData.data.errMsg == '登录过期') {
              wx.navigateTo({
                url: '/pages/authorized-login/authorized-login?type=0&id=' + that.data.productId,
              })
            }
          }, 1000)
        }

      })
    } else {
      wx.navigateTo({
        url: '../authorized-login/authorized-login?type=0&id=' + that.data.productId,
      })
    }
    
  },
  getProductMsg: function () {
    console.log(getApp().globalData.appId)
    let that = this
    let url = URL.HTTP_BASE_INTRANET + SER_KEY.SER_KEY4;
    let data = {
      appId: getApp().globalData.appId,
      productId: that.data.productId,
      shopId: that.data.shopId
    };
    GET(url, data, (resData) => {
      console.log(resData)
      if (resData.data.res == "SUCCESS"){
        wx.stopPullDownRefresh()
        let productData = resData.data.product
        // let defaultLattributeName = '';
        let shop = resData.data.shop
        let price1 = productData.lspecification[0].price
        let price2 = productData.lspecification[productData.lspecification.length - 1].price
        let end = 0
        price1 < price2 ? end = price1 + '-' + price2 : end = price2 + '-' + price1
        let detailPic=[]
        // console.log(productData.pic.length)
        detailPic=productData.pic.slice(1,productData.pic.length)
        // console.log(detailPic)
        that.setData({
          productMsg: productData,
          detailPic:detailPic,
          choosePrice: end,
          shopName: shop.shop_name,
          shopImage: shop.shop_image,
          contactCode: resData.data.shop.contact_qcode
        }, () => that.getProductImg(productData.pic[0].pic))
      }
      
    })
  },
  //轮播图的切换事件  
  swiperChange: function (e) {
    console.log(e);
    this.setData({
      swiperCurrent: e.detail.current   //获取当前轮播图片的下标
    })
  },
  // 选择规格
  choose: function (e) {
    this.setData({
      select_num:1
    })
    let that = this
    let animal1 = wx.createAnimation({
      timingFunction: 'ease-in'
    }).translate(0, -1200).step({
      duration: 400
    })
    that.setData({
      // showBox: true,
      // choose_spec_item: e.currentTarget.dataset.item,
      // index: e.currentTarget.dataset.index,
      // parentindex: e.currentTarget.dataset.parentindex,
      animationData: animal1.export(),
    })
    setTimeout(function () {
      that.setData({
        show: true
      })
    }, 400);
  },
  //选中样式
  choosetype: function (e) {
    let that = this;
    let name = that.data.choosedLattribute
    let pic=e.currentTarget.dataset.pic
    let price = e.currentTarget.dataset.price
    console.log(pic)
    that.setData({
      typeIndex: e.currentTarget.dataset.index,
      choosed: name ? e.currentTarget.dataset.name + '/' + name : e.currentTarget.dataset.name,
      choosePrice: (price * this.data.select_num).toFixed(2) ,
      oriPrice: price,
      choosedSpecId: e.currentTarget.dataset.id,
      choosedLspecification: e.currentTarget.dataset.name,
      choosePic:pic
    })
  },
  // 增加
  HZL_jia: function (e) {
    // console.log(e);
    let num = this.data.select_num
    let price=this.data.oriPrice
    if(price==null){
      wx.showToast({
        title: '请选择规格',
        icon:'none'
      })
      return;
    }
    num++
    this.setData({
      select_num: num,
      choosePrice:(num*price).toFixed(2)
    })
  },

  //减少
  HZL_jian: function (e) {
    // console.log(e);
    let num = this.data.select_num
    if (this.data.select_num > 1) {
      num--
      this.setData({
        select_num: num,
        choosePrice:(num*this.data.oriPrice).toFixed(2)
      })
    } else {
      wx.showToast({
        title: '不能再少了！',
        icon: 'none'
      });
    }

    
  },
  // 加入购物车
  addShopCart: function () {
    var that = this;
      var shopId = that.data.shopId;
      var choosedAttrName = '';
      var sessionId = wx.getStorageSync('session_id');
    if (sessionId!=''){
      let num = that.data.select_num;
      let url = URL.HTTP_BASE_INTRANET + SER_KEY.SER_KEY5;
      if (that.data.productMsg.lattribute && that.data.productMsg.lattribute.length) {
        choosedAttrName = that.data.choosedLspecification + '-' + that.data.choosedLattribute;
      } else {
        choosedAttrName = that.data.choosedLspecification;
      }
      let data = {
        appId: getApp().globalData.appId,
        sessionId: sessionId,
        shopId: shopId,
        productId: that.data.productMsg.id,
        productCount: num,
        attrName: choosedAttrName||'默认',
        specId: that.data.choosedSpecId||that.data.productMsg.lspecification[0].id
      };
      console.log(data);
      GET(url, data, (resData) => {
        console.log(resData);
        if (resData.data.res == "SUCCESS") {
          wx.showToast({
            title: '加入购物车成功！',
          })
          setTimeout(function () {
            let animal1 = wx.createAnimation({
              timingFunction: 'ease-in'
            }).translate(0, 1200).step({
              duration: 500
            })
            that.setData({
              animationData: animal1.export(),
              show: false,
            })
            wx.showTabBar({})
          }, 1000);

        }else{
          wx.showToast({
            title: resData.data.errMsg,
            icon:'none'
          })
          setTimeout(function () {
            if (resData.data.errMsg == '登录过期') {
              wx.navigateTo({
                url: '/pages/authorized-login/authorized-login?type=0&id=' + that.data.productId,
              })
            }
          }, 1000)
        }

      })
    }else{
      wx.navigateTo({
        url: '../authorized-login/authorized-login?type=0&id='+that.data.productId,
      })
    }
     
    

  },
  close: function () {
    let that = this
    let animal1 = wx.createAnimation({
      timingFunction: 'ease-in'
    }).translate(0, 1200).step({
      duration: 400
    })
    let animal2 = wx.createAnimation({
      timingFunction: 'ease-in'
    }).translate(0, 1200).step({
      duration: 400
    })
    that.setData({
      animationData: animal1.export(),
      animationData2: animal2.export(),
      contBox:false
    })
    setTimeout(function () {
      that.setData({
        show: false
      })
    }, 200);
  },
  shares:function(){
    let that = this
    let animal2 = wx.createAnimation({
      timingFunction: 'ease-in'
    }).translate(0, -1200).step({
      duration: 400
    })
    that.setData({
      animationData2: animal2.export(),
    })
    setTimeout(function () {
      that.setData({
        show: true
      })
    }, 200);
  },
  cancel:function(){
    let that = this
    let animal2 = wx.createAnimation({
      timingFunction: 'ease-in'
    }).translate(0, 1200).step({
      duration: 400
    })
    that.setData({
      animationData2: animal2.export(),
    })
    setTimeout(function () {
      that.setData({
        show: false
      })
    }, 200);
  },
  getUserInfo(){
    wx.getUserInfo({    //获取微信用户信息
      success: function (res) {
        this.getImageInfo(res.userInfo.avatarUrl);  //  调取图片处理方法
        this.setData({
          userName: res.userInfo.nickName
        });
      }
    });

  },
  getImageInfo(url) {    //  图片缓存本地的方法
    if (typeof url === 'string') {
      wx.getImageInfo({   //  小程序获取图片信息API
        src: url,
        success: function (res) {
          this.setData({
            head_img: res.path
          })
        },
        fail(err) {
          console.log(err)
        }
      })
    }
  },
  showPoster(){
    let that=this
    let animal2 = wx.createAnimation({
      timingFunction: 'ease-in'
    }).translate(0, 1200).step({
      duration: 400
    })
    that.setData({
      animationData2: animal2.export(),
    })
    setTimeout(function () {
      that.setData({
        sharePoster: false,
        show: true
      })
    }, 200);
    that.getwxacode()
    
  },
  posterClose: function () {
    this.setData({
      sharePoster: true,
      show:false
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log('测试')
    let appId = getApp().globalData.appId
    console.log(appId)
    if(appId==null){
      this.getAppid()
    }else{
      // this.getContactCode()
      this.getProductMsg()
    }
  },
  getAppid: function () {
    let that = this
    console.log('eee')
    if (wx.getExtConfig) {
      wx.getExtConfig({
        success: function (res) {
          //给全局变量appId赋值
          getApp().globalData.appId = res.extConfig.appId
          that.getProductMsg()
          // that.getContactCode()
        },
        fail: function () {

        }
      })
    } else {
      wx.showModal({
        title: '提示',
        content: "微信版本太低,无法使用小程序，请及时升级您的微信。",
        showCancel: false,
        success: function (res) {
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getProductMsg()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
 * 用户点击右上角分享
 */
  onShareAppMessage: function (ops) {
    // console.log(ops);
    var id = this.data.productMsg.id;
    var shopId = this.data.shopId
    var storeMsg = this.data.storeMsg
    console.log(id);   
    wx.setStorageSync('id', this.data.shopId)
    return {
      title: this.data.productMsg.name,
      // desc: '分享页面的内容',
      path: '/pages/product-details/product-details?id=' + id & 'shopId=' + shopId & 'shopName=' + storeMsg.shop_name & 'shopImage=' + storeMsg.shop_image, // 路径，传递参数到指定页面。
      imageUrl: this.data.pic[0].pic,                    
      success: function (res) {
        console.log(res)
        wx.showToast({
          title: '转发成功',
          icon: 'none'
        })
      },
      fail: function (res) {
        // console.log(res)
      }
    }
  },
})